package com.yumeko.springcloud.controller;

import com.yumeko.springcloud.entity.TPayment;
import com.yumeko.springcloud.lib.Loadbalanced;
import com.yumeko.springcloud.util.ResponseData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.net.URI;
import java.util.List;

/**
 * @program: SpringCloud
 * @description:
 * @author: Zheng Hao
 * @create: 2020-03-07 05:53
 */
@RestController
@Slf4j
public class OrderController {
    @Resource
    private RestTemplate restTemplate;
    @Resource
    private DiscoveryClient discoveryClient;
    @Resource
    private Loadbalanced loadbalanced; //引入自己的

    @PostMapping("/consumer/payment/insert")
    @ResponseBody
    public ResponseData insertPayment(@RequestBody TPayment tPayment) {
        ResponseEntity<ResponseData> forEntity = restTemplate.postForEntity("http://CLOUD-PROVIDER-SERVICE/tPayment/insert", tPayment, ResponseData.class);
        if (forEntity.getStatusCode().is2xxSuccessful()) {
            return forEntity.getBody();
        } else {
            return new ResponseData(500, "远程服务器错误!");
        }
    }

    @GetMapping("/consumer/payment/selectOne")
    public ResponseData<TPayment> selectOnePayment(Long id) {
        ResponseEntity<ResponseData> forEntity = restTemplate.getForEntity("http://CLOUD-PROVIDER-SERVICE/tPayment/selectOne?id=" + id, ResponseData.class);
        if (forEntity.getStatusCode().is2xxSuccessful()) {
            return forEntity.getBody();
        } else {
            return new ResponseData(500, "远程服务器错误!");
        }
    }

    @PostMapping("/consumer/payment/insert1")
    @ResponseBody
    public ResponseData insertPayment1(@RequestBody TPayment tPayment) {
        return restTemplate.postForObject("http://CLOUD-PROVIDER-SERVICE/tPayment/insert", tPayment, ResponseData.class);
    }

    @GetMapping("/consumer/payment/selectOne1")
    public ResponseData selectOnePayment1(Long id) {
        return restTemplate.getForObject("http://CLOUD-PROVIDER-SERVICE/tPayment/selectOne?id=" + id, ResponseData.class);
    }

    @GetMapping("/consumer/payment/lb")
    public String getPaymentLB() {
        List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PROVIDER-SERVICE");
        if (instances == null || instances.size()<=0) {
            return null;
        }else {
            ServiceInstance instance = loadbalanced.instance(instances);
            URI instanceUri = instance.getUri();
            return restTemplate.getForObject(instanceUri+"/tPayment/payment/lb",String.class);
        }
    }
    //http://localhost:8000/consumer/payment/zipkin
    @GetMapping("/consumer/payment/zipkin")
    public String paymentZipkin(){
        String result = restTemplate.getForObject("http://localhost:8001" + "/tPayment/payment/zipkin/", String.class);
        return result;
    }
}
